﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Templates_Article_Default : BaseTemplatePage
{
    protected override void AttachEvents()
    {
        acmsDownload1.DataBound += new EventHandler(acmsDownload1_DataBound);
        acmsDownload2.DataBound += new EventHandler(acmsDownload2_DataBound);
        acmsAuthor.DataBound += new EventHandler(acmsAuthor_DataBound);
        ucKeyPeople.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Post>>(ucKeyPeople_SelectData);
    }

    void ucKeyPeople_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        e.SetData(Post.GetAllBySiteIdAndParentPath(AcmsContext.SiteId, "~/keypeople/feeearners/", AcmsContext.SiteVersion));
    }

    void acmsAuthor_DataBound(object sender, EventArgs e)
    {
        liAuthor.Visible = !acmsAuthor.IsEmpty;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    protected string SectionClass { get; private set; }

    void acmsDownload2_DataBound(object sender, EventArgs e)
    {
        if (acmsDownload2.IsEmpty)
        {
            liDownload2.Visible = false;
        }
    }

    void acmsDownload1_DataBound(object sender, EventArgs e)
    {
        if (acmsDownload1.IsEmpty)
        {
            liDownload1.Visible = false;
        }
    }

    public override void DataBind()
    {
        if (AcmsContext.CurrentPost != null)
        {
            //Find from the path if what the section class and title should be
            string[] nodes = AcmsContext.CurrentPost.Path.Split('/');
            if (nodes.Length >= 3)
            {
                SectionClass = string.Concat(nodes[1].ToLower(), nodes[2]);
            }

            if (nodes.Length == 3)
            {
                lblTitle.Text = AcmsContext.CurrentPost.Parent.DisplayName;
            }
            else if (nodes.Length == 2)
            {
                lblTitle.Text = AcmsContext.CurrentPost.DisplayName;
                articleTitle.Visible = false;
            }
            else if (nodes.Length > 1 && (nodes[1].ToLower() == "services" || nodes[1].ToLower() == "sectors"))
            {
                Post root = Post.GetBySiteIdAndPath(AcmsContext.SiteId, String.Concat(nodes[0], "/", nodes[1], "/", nodes[2]), AcmsContext.CurrentPost.SiteVersion);
                if (root != null)
                {
                    lblTitle.Text = root.DisplayName;
                }
            }
            else if (nodes.Length > 1)
            {
                Post root = Post.GetBySiteIdAndPath(AcmsContext.SiteId, String.Concat(nodes[0], "/", nodes[1]), AcmsContext.CurrentPost.SiteVersion);
                if (root != null)
                {
                    lblTitle.Text = root.DisplayName;
                }
            }
        }

        List<Topic> allTopics = Topic.GetAllByPostId(AcmsContext.PostId);
        List<Topic> services = new List<Topic>();
        List<Topic> sectors = new List<Topic>();
        List<Topic> people = new List<Topic>();
        foreach (Topic t in allTopics)
        {
            switch (t.TopicGroupName)
            {
                case "Services":
                    services.Add(t);
                    break;
                case "Sectors":
                    sectors.Add(t);
                    break;
                case "People":
                    people.Add(t);
                    break;
            }
        }
        if (services.Count > 0)
        {
            listServices.DataSource = services;
        }
        else
        {
            listServices.Visible = false;
        }
        if (sectors.Count > 0)
        {
            listSectors.DataSource = sectors;
        }
        else
        {
            listSectors.Visible = false;
        }

        if (people.Count > 0)
        {
            List<Post> peoplePosts = new List<Post>();
            foreach (Topic t in people)
            {
                Post p = Post.GetBySiteIdAndPath(AcmsContext.SiteId, string.Format("~/keypeople/feeearners/{0}", t.Name.Replace(" ", "").Trim()), AcmsContext.SiteVersion);
                if (p == null)
                {
                    p = Post.GetBySiteIdAndPath(AcmsContext.SiteId, string.Format("~/keypeople/administrators/{0}", t.Name.Replace(" ", "").Trim()), AcmsContext.SiteVersion);
                }
                if (p != null)
                {
                    peoplePosts.Add(p);
                }
            }

            //if no people posts linked through topics show the Key people contol
            if (peoplePosts.Count == 0)
            {
                ucKeyPeople.Visible = true;
                dlPeople.Visible = false;
            }
            else
            {
                ucKeyPeople.Visible = false;
                dlPeople.Visible = true;
                dlPeople.DataSource = peoplePosts;
            }
        }


        base.DataBind();
    }
}
